// dates management

#include <extend.h>

//------------------------------------------------------------------------//

LONG greg2julian( WORD wDay, WORD wMonth, int iYear )
{
   int iFactor = ( wMonth < 3 ) ? -1: 0;

   return ( 1461 * ( iFactor + 4800 + iYear ) / 4 ) +
          ( ( wMonth - 2 - ( iFactor * 12 ) ) * 367 ) / 12 -
          ( 3 * ( ( iYear + 4900 + iFactor ) / 100 ) / 4 ) +
          wDay - 32075;
}

//------------------------------------------------------------------------//

void julian2greg( LONG julian, WORD * pwDay, WORD * pwMonth, int * pwYear )
{
  LONG U,V,W,X;

  if( ! julian )
  {
     * pwDay   = 0;
     * pwMonth = 0;
     * pwYear  = 0;
  }
  else
  {
     julian += 68569;
     W = ( julian * 4 ) / 146097;
     julian -= ( ( 146097 * W ) + 3 ) / 4;
     X = 4000 * ( julian + 1 ) / 1461001;
     julian -= ( ( 1461 * X ) / 4 ) - 31;
     V = 80 * julian / 2447;
     U = V / 11;
     * pwDay   = julian - ( 2447 * V / 80 );
     * pwMonth = V + 2 - ( U * 12 );
     * pwYear  = X + U + ( W - 49 ) * 100;
  }
}

//------------------------------------------------------------------------//

CLIPPER GREG2JULIA() // N()
{
   _retnl( greg2julian( _parni( 1 ), _parni( 2 ), _parni( 3 ) ) );
}

//------------------------------------------------------------------------//

CLIPPER JULIAN2GRE() // G()
{
   WORD wDay, wMonth;
   int  iYear;

   julian2greg( _parni( 1 ), &wDay, &wMonth, &iYear );
   _storni( 2, wDay );
   _storni( 3, wMonth );
   _storni( 4, iYear );
}

//------------------------------------------------------------------------//